package org.nokatag.system;

import javax.servlet.http.HttpServletResponse;

/**
 * form返回参数工具类
 * @author rebin
 *
 */
public class FormUtil {

	private static final String SHOW_ERRO = "erro";
	private static final String SHOW_SUCCESS = "success";
	
	/**
	 * 显示成功信息
	 * @param msg 显示的消息体
	 */
	public static void ReturnSuccess(String msg){
		WriteMsg(SHOW_SUCCESS,msg,null);
	}
	/**
	 * 显示成功信息
	 * @param msg 显示的消息体
	 * @param ahid 多少秒以后自动关闭消息
	 */
	public static void ReturnSuccess(String msg,Integer ahid){
		WriteMsg(SHOW_SUCCESS,msg,ahid);
	}
	/**
	 * 显示错误信息
	 * @param msg 显示的消息体
	 */
	public static void ReturnErro(String msg){
		WriteMsg(SHOW_ERRO,msg,null);
	}
	/**
	 * 显示错误信息
	 * @param msg 显示的消息体
	 * @param ahid 多少秒以后自动关闭消息
	 */
	public void ReturnErro(String msg,Integer ahid){
		WriteMsg(SHOW_ERRO,msg,ahid);
	}
	/**
	 * 返回自定义JSON结构
	 * @param json JSON消息体
	 */
	public static void ReturnJson(String json){
		WriteMsg(json);
	}
	
	private static void WriteMsg(String msg){
		try{
			if(msg==null)
				return;
			HttpServletResponse response = ServletNokaContext.getResponse();
			response.setContentType("");
			response.getOutputStream().write(msg.toString().getBytes(Concest.ENCODING));
		}catch(Exception se){
			BugInfoWrint.Print(se);
		}	
	}
	private static void WriteMsg(String type,String msg,Integer ahid){
		StringBuffer body = new StringBuffer();//{s:'erro',m:'msg',a:3}
		body.append("{");
		body.append("s:'"+type+"',");
		if(ahid!=null)
			body.append("a:'"+ahid+"',");
		body.append("m:'"+msg.replace("\'","\\'")+"'");
		body.append("}");
		try{
			HttpServletResponse response = ServletNokaContext.getResponse();
			response.setContentType("text/javascript;charset=UTF-8");
			response.getOutputStream().write(body.toString().getBytes(Concest.ENCODING));
		}catch(Exception se){
			BugInfoWrint.Print(se);
		}
	}
	
}
